<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */

$isSpecial = (int)$_data["isSpecial"];
$intSpecialNumType = (int)$_data["SpecialNumType"];
$isRealShow = (int)$_data["isRealShow"];
$intGambleID = (int)$_data["GambleID"];
if(strlen($isSpecial) == 0)$isSpecial = 0;
if(strlen($isRealShow) != 1)$isRealShow = 0;
$strBT = trim($_data["Bet_Type"]);
$arrBType = explode(",", $strBT);
$arrOtherMsg = explode(",", trim($_data["OtherMsg"]));
$strLottoNumber = trim($_data["strLottoNumber"]);

$arrSumStake = array();
$arrSumPayOff = array();
$arrSumCommission = array();

$strSQL = "select * from `bet_TodayGame".$intLID."` where LottoID = ".$intLID." and GambleID = '".$intGambleID."' and Bet_Type in (".$strBT.") ";
$strRealTip = ($isRealShow == 0)?"":"Real";
switch($strBT){
	case "7":
	case "8":
	case "9":
	case "10":
	case "11":
	case "13":
		$intBT = (int)$strBT;

		//找出三定位 1000 個號碼的總注單量
		$strSumSQL = "select SUM(tolWager + tolSuppleWager) as tolWager, SUM(tol".$strRealTip."Stake - tolSuppleStake) as tolStake, SUM(tol".$strRealTip."PayOff - tolSupplePayOff) as tolPayOff, SUM(tolCommission - tolSuppleCommission) as tolCommission from `bet_TodayGame".$intLID."` where LottoID = ".$intLID." and GambleID = '".$intGambleID."' and Bet_Type = ".$intBT;
		if(!$RS = PMA_mysql_query($strSumSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find TodayGame Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
			ErrMsgOutputHTML("Error", "", 1);
		}
		$Rows = PMA_mysql_fetch_object($RS);
		$arrSumStake[$intBT] = $Rows -> tolStake;
		$arrSumPayOff[$intBT] = $Rows -> tolPayOff;
		$arrSumCommission[$intBT] = $Rows -> tolCommission;

		switch($arrOtherMsg[0]){
			case "Sort": //排序
				$strSQL = "select *, (".($Rows -> tolStake - $Rows -> tolCommission)." - tol".$strRealTip."Stake - tol".$strRealTip."PayOff + tolSuppleStake + tolSupplePayOff) as preWin from `bet_TodayGame".$intLID."` where LottoID = ".$intLID." and GambleID = '".$intGambleID."' and Bet_Type = ".$intBT." order by preWin, Number limit ";
				$strSQL .= ($strBT != "13")?"100":"220";
				break;

			default:
				if($intBT == 13){
					$strSQL .= "order by Number";
				}else{
					$strSQL .= "and Number in (".$strLottoNumber.") order by Number";
				}
				break;
		}
		break;

	default:
		if($arrOtherMsg[0] == "AllSort"){
			

			$arrSQL1 = array();
			$arrSQL2 = array();
			foreach($arrBType as $intBTa){
				$arrSQL1[] = "(select *, ({tolStake_Commission".$intBTa."} - tol".$strRealTip."Stake - tol".$strRealTip."PayOff + tolSuppleStake + tolSupplePayOff) as preWin from `bet_TodayGame".$intLID."` where LottoID = ".$intLID." and GambleID = '".$intGambleID."' and Bet_Type = ".$intBTa." order by preWin, Number limit 30)";

				$arrSQL2[] = "(select Bet_Type, SUM(tolWager + tolSuppleWager) as tolWager, SUM(tol".$strRealTip."Stake - tolSuppleStake) as tolStake, SUM(tol".$strRealTip."PayOff - tolSupplePayOff) as tolPayOff, SUM(tolCommission - tolSuppleCommission) as tolCommission from `bet_TodayGame".$intLID."` where LottoID = ".$intLID." and GambleID = '".$intGambleID."' and Bet_Type = ".$intBTa." group by Bet_Type)";
			}
			//統計總排名，各下注種類注金總額
			$strSQL = implode(" union ", $arrSQL2);
			if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
				$GLOBALS["errormessage"] = "Find TodayGame Data Error!!";
				$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
				ErrMsgOutputHTML("Error", "", 1);
			}
			while($Rows = PMA_mysql_fetch_object($RS)){
				$arrSumStake[$Rows -> Bet_Type] = $Rows -> tolStake;
				$arrSumPayOff[$Rows -> Bet_Type] = $Rows -> tolPayOff;
				$arrSumCommission[$Rows -> Bet_Type] = $Rows -> tolCommission;
			}

			$strSQL = implode(" union ", $arrSQL1);
			foreach($arrSumStake as $intBTa => $flStake){
				$strSQL = str_replace("{tolStake_Commission".$intBTa."}", $flStake - $arrSumCommission[$intBTa], $strSQL);
			}
			//print $strSQL;

		}else{ //if($arrOtherMsg[0] == "AllSort"){
			$strSQL .= "order by Bet_Type";
		}
		break;
}
//print $strSQL;

if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Find TodayGame Data Error!!";
	$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

if(PMA_mysql_num_rows($RS) == 0){
	$GLOBALS["errormessage"] = "No TodayGame Data!!";
	ErrMsgOutputHTML("Error", "", 1);
}
	
$intNum = -1;
while($Rows = PMA_mysql_fetch_object($RS)){
	if($isRealShow == 0){
		$tolStake = $Rows -> tolStake;
		$tolPayOff = $Rows -> tolPayOff;
		$tolWager = $Rows -> tolWager;
		$tolCommission = 0;
	}else{
		$tolStake = $Rows -> tolRealStake;
		$tolPayOff = $Rows -> tolRealPayOff;
		$tolWager = $Rows -> tolWager;
		$tolCommission = $Rows -> tolCommission;
	}
	if(empty($tolStake))$tolStake = 0;
	if(empty($tolPayOff))$tolPayOff = 0;
	if(empty($tolCommission))$tolCommission = 0;
	if(empty($tolWager))$tolWager = 0;
	$tolStake -= $Rows -> tolSuppleStake;
	$tolPayOff -= $Rows -> tolSupplePayOff;
	$tolCommission -= $Rows -> tolSuppleCommission;

	if($intBT != $Rows -> Bet_Type)$intNum = -1;
	$intBT = $Rows -> Bet_Type;
	$intApointNum = $_arrGLineNumToApointNum[$Rows -> LottoID][$Rows -> Bet_Type][$Rows -> Number];
	if($intApointNum == null)$intApointNum = 0;
	$intNum = ($Rows -> Bet_Type >= 7 && $Rows -> Bet_Type <= 11 ||  $Rows -> Bet_Type == 13 || $arrOtherMsg[0] == "AllSort")?($intNum+1):$Rows -> Number;

	$strCon .= "parent.Game".$intBT."[".$intNum."]=new Array('".$Rows -> LottoID."','".$Rows -> GambleID."','".$intBT."','".$Rows -> Number."','".$Rows -> OddsID."','".round(($Rows -> Odds + $_arrGambleType[$Rows -> Bet_Type][$intApointNum]), 3)."','".$Rows -> isStopBet."','".$tolWager."','".$tolStake."','".$tolPayOff."','".$tolCommission."');\n";

	switch($Rows -> Bet_Type){
		case 7:
		case 8:
		case 9:
		case 10:
		case 11:
		case 13:
			break;

		case 14:
		case 15:
		case 16:
		case 17:
			$intTmp = floor($Rows -> Number / 10);
			$arrSumStake[$Rows -> Bet_Type][$intTmp] += $tolStake;
			$arrSumPayOff[$Rows -> Bet_Type][$intTmp] += $tolPayOff;
			$arrSumCommission[$Rows -> Bet_Type][$intTmp] += $tolCommission;
			break;

		default:
			if($arrOtherMsg[0] != "AllSort"){
				$arrSumStake[$Rows -> Bet_Type] += $tolStake;
				$arrSumPayOff[$Rows -> Bet_Type] += $tolPayOff;
				$arrSumCommission[$Rows -> Bet_Type] += $tolCommission;
				//$arrSumWStake[$Rows -> Bet_Type] += $tolStake + $Rows -> tolSuppleStake;
			}
			break;
	}
}

/*=================================================================*/
header("Cache-Control: no-store, no-cache, must-revalidate");	  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");															  // HTTP/1.0
?><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript">
<!--
<?php 
print $strCon."\n";
print "parent.isEmergency = ".((empty($_isStopAllBet))?"0":$_isStopAllBet).";\n";
print "parent.changEmergency();\n";

foreach($arrSumStake as $intBT => $val){
	if(is_array($val)){
		foreach($val as $intN => $val2){
			print "parent.sumStake[".$intBT."][".$intN."] = ".round($val2).";\n";
			print "parent.sumPayOff[".$intBT."][".$intN."] = ".round($arrSumPayOff[$intBT][$intN]).";\n";
			print "parent.sumCommission[".$intBT."][".$intN."] = ".round($arrSumCommission[$intBT][$intN]).";\n";
		}
	}else{
		print "parent.sumStake[".$intBT."] = ".round($arrSumStake[$intBT]).";\n";
		print "parent.sumPayOff[".$intBT."] = ".round($arrSumPayOff[$intBT]).";\n";
		print "parent.sumCommission[".$intBT."] = ".round($arrSumCommission[$intBT]).";\n";
	}
}

switch($strBT){
	//定位
	case "14":
	case "15":
	case "18":
	case "19":
	case "20":
	case "21":
	case "22":
	case "14,15,18,19,20,21,22":
		//print "parent.sumWStake[".$strBT."] = ".round($arrSumWStake[$strBT]).";\n";
		print "parent.ShowNumberOdds18(18);\n";
		break;

	//二定位
	case "1":
	case "2":
	case "3":
	case "4":
	case "5":
	case "6":
	case "12":
	case "1,2,3,4,5,6":
		if($arrOtherMsg[0] == "AllSort"){
			print "parent.ShowTwinNumberSort();\n";
		}else{
			print "parent.ShowTwinNumber(".$strBT.");\n";
		}
		break;

	case "16":
	case "17":
	case "16,17":
		print "parent.ShowNumberOdds16(16);\n";
		break;

	case "7":
	case "8":
	case "9":
	case "10":
	case "7,8,9,10,12,13":
		if($arrOtherMsg[0] == "AllSort"){
			print "parent.ShowTrinityNumberSort();\n";
		}else{
			print "parent.ShowTrinityNumber(".$strBT.");\n";
		}
		break;

	case "11":
		print "parent.ShowFourNumber(11);\n";
		break;

	case "13":
		print "parent.Show3WordNumber(13);\n";
		break;
}
?>
//-->
</script>